﻿using Aierp.SystemEntities.System;
using Aierp.Utility;
using Aierp.Utility.Data;
using Chloe;

namespace Aierp.SystemRepository.Implements
{
    /// <summary>
    /// 词典分类
    /// </summary>
    public class DicCategoryRepository : DbInstance
    {
        /// <summary>
        /// 查询一个名称--根据名称和编码
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public Sys_DicCategory Find(string title, string code)
        {
            var result = this.FindBy<Sys_DicCategory>(c => c.Title == title);
            if (result != null) return result;

            result = this.FindBy<Sys_DicCategory>(c => c.Code == code);
            return result;
        }

        /// <summary>
        /// 查询一个编码-根据ID，只返回指定信息，密码等关键信息不返回
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Sys_DicCategory FindById(int key)
        {
            return this.FindBy<Sys_DicCategory>(c => c.KeyId == key);
        }

        /// <summary>
        /// 获取一个列表，供给Grid使用
        /// </summary>
        /// <param name="sortOrder"></param>
        /// <param name="limit"></param>
        /// <param name="offset"></param>
        /// <param name="searchTxt"></param>
        /// <returns></returns>
        public string GetList(string sortOrder, int limit, int offset,string searchTxt)
        {
            if (!string.IsNullOrEmpty(searchTxt))
            {
                return this.GetPageList<Sys_DicCategory, object>(limit, offset, c => c.Title.Contains(searchTxt) || c.Code.Contains(searchTxt), c => c.Sortnum, sortOrder);
            }
            return this.GetPageList<Sys_DicCategory, object>(limit, offset, null, c => c.Sortnum, sortOrder);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Add(Sys_DicCategory item)
        {
            if (item == null) return 0;

            var model = this.Find(item.Title, item.Code);
            if (model != null) return -2;

            var ret = Db.Insert(item);

            return ret?.KeyId ?? 0;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Update(Sys_DicCategory item)
        {
            //先检查是否存在
            var model = this.FindById(item.KeyId);
            if (model == null) return -2;

            //检查名称和编码是否同时存在
            model = this.Find(item.Title, item.Code);
            if (model != null) return -9;

            //更新
            Db.TrackEntity(model);//在上下文中跟踪实体这时只会更新被修改的字段
            model.Code = item.Code;
            model.Title = item.Title;
            model.Sortnum = item.Sortnum;
            return Db.Update(model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int Remove(int key)
        {
            //先检查是否存在
            var result = this.FindById(key);
            if (result == null) return -2;

            //要判断是否子类是否使用了该编码，如果字典表中已经使用了该分类，则不允许删除该分类。
            var dicsRepo = new DicsRepository();
            var model = dicsRepo.FindByCategoryId(key);
            if (model != null) return -3;

            //删除
            var ret = Db.DeleteByKey<Sys_DicCategory>(key);

            return ret;
        }
    }
}
